use std::path::PathBuf;

use crate::flags::{Flags, SubCommand};


#[derive(Debug, Default, Clone)]
pub struct Config {
    pub initial_cargo: PathBuf,
    pub initial_rustc: PathBuf,

    pub cmd: SubCommand,
    pub src: PathBuf,
    pub out: PathBuf,
    pub paths: Vec<PathBuf>,
}

impl Config {
    pub fn default() -> Self {
        Config {
            cmd: SubCommand::default(),
            src: {
                let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
                manifest_dir.parent().unwrap().parent().unwrap().to_owned()
            },
            out: PathBuf::from("build"),
            paths: Vec::new(),
            ..Default::default()
        }
    }

    pub fn parse(args: &[String]) -> Config {
        Self::parse_inner(args)
    }

    pub fn parse_inner(args: &[String]) -> Config {
        let mut flags = Flags::parse(args);
        let mut config = Config::default();

        config.paths = std::mem::take(&mut flags.paths);
        config.cmd = flags.cmd;
        
        config
    }
}